package com.android.gufengmall.login;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.RequiresApi;
import androidx.cardview.widget.CardView;

import com.android.gufengmall.App;
import com.android.gufengmall.BaseActivity;
import com.android.gufengmall.R;
import com.android.gufengmall.bean.UserBean;
import com.android.gufengmall.gen.DaoSession;
import com.android.gufengmall.gen.UserBeanDao;
import com.android.gufengmall.utils.LogUtils;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.List;

public class RegisterActivity extends BaseActivity {

    private FloatingActionButton mFloatingActionButton;
    private CardView mCardViewAdd;
    private Button mBtnGo;
    private EditText mEdtUser;
    private EditText mEdtPass;
    private EditText mEdtRepeatPass;

    private App mMyApp;
    private DaoSession mDaoSession;
    private UserBeanDao mUserDao;
    private List<UserBean> mUsers;
    private EditText mEtAge;
    private EditText mEtSex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            ShowEnterAnimation();
        }

        mMyApp = (App) getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mUserDao = mDaoSession.getUserBeanDao();

        initView();
        initListener();
    }

    private void initListener() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mFloatingActionButton.setOnClickListener(v -> animateRevealClose());
        }
        mBtnGo.setOnClickListener(v -> {
            String user = mEdtUser.getText().toString().trim();
            String pass = mEdtPass.getText().toString().trim();
            String repeatPass = mEdtRepeatPass.getText().toString().trim();
            String sex = mEtSex.getText().toString().trim();
            String age = mEtAge.getText().toString().trim();
            if (pass.isEmpty() || user.isEmpty() || repeatPass.isEmpty() || sex.isEmpty() || age.isEmpty()) {
                showToast("注册信息不能为空");
            } else if (!pass.equals(repeatPass)) {
                showToast("2次密码输入不一致，请检查");
            } else {
                mUsers = mUserDao.loadAll();
                LogUtils.d("RegisterActivity", "mUsers.size()--->" + mUsers.size());
                if (mUsers.isEmpty()) {
                    UserBean userBean = new UserBean();
                    userBean.setIsAdmin(false);
                    userBean.setUserName(user);
                    userBean.setUserPass(pass);
                    userBean.setSex(sex);
                    userBean.setAge(age);
                    mUserDao.insert(userBean);
                    showToast("注册成功");
                    startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                    finish();
                } else {
                    for (int i = 0; i < mUsers.size(); i++) {
                        UserBean user1 = mUsers.get(i);
                        if (user1.getUserName().equals(user)) {
                            showToast("注册失败，账号已存在");
                        } else {
                            UserBean userBean = new UserBean();
                            userBean.setIsAdmin(false);
                            userBean.setUserName(user);
                            userBean.setUserPass(pass);
                            userBean.setSex(sex);
                            userBean.setAge(age);
                            mUserDao.insert(userBean);
                            showToast("注册成功");
                            startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                            finish();
                        }
                    }
                }
            }
        });
    }

    private void initView() {
        mEdtUser = findViewById(R.id.et_username);
        mEdtPass = findViewById(R.id.et_password);
        mEdtRepeatPass = findViewById(R.id.et_repeatpassword);
        mFloatingActionButton = findViewById(R.id.fab);
        mCardViewAdd = findViewById(R.id.cv_add);
        mEtAge = findViewById(R.id.et_age);
        mEtSex = findViewById(R.id.et_sex);
        mBtnGo = findViewById(R.id.bt_go);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void ShowEnterAnimation() {
        Transition transition = TransitionInflater.from(RegisterActivity.this).inflateTransition(R.transition.fabtransition);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setSharedElementEnterTransition(transition);
        }

        transition.addListener(new Transition.TransitionListener() {
            @Override
            public void onTransitionStart(Transition transition) {
                mCardViewAdd.setVisibility(View.GONE);
            }

            @Override
            public void onTransitionEnd(Transition transition) {
                transition.removeListener(this);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    animateRevealShow();
                }
            }

            @Override
            public void onTransitionCancel(Transition transition) {

            }

            @Override
            public void onTransitionPause(Transition transition) {

            }

            @Override
            public void onTransitionResume(Transition transition) {

            }


        });
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void animateRevealShow() {
        Animator mAnimator = ViewAnimationUtils.createCircularReveal(mCardViewAdd, mCardViewAdd.getWidth() / 2, 0, mFloatingActionButton.getWidth() / 2, mCardViewAdd.getHeight());
        mAnimator.setDuration(500);
        mAnimator.setInterpolator(new AccelerateInterpolator());
        mAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }

            @Override
            public void onAnimationStart(Animator animation) {
                mCardViewAdd.setVisibility(View.VISIBLE);
                super.onAnimationStart(animation);
            }
        });
        mAnimator.start();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void animateRevealClose() {
        Animator mAnimator = ViewAnimationUtils.createCircularReveal(mCardViewAdd, mCardViewAdd.getWidth() / 2, 0, mCardViewAdd.getHeight(), mFloatingActionButton.getWidth() / 2);
        mAnimator.setDuration(500);
        mAnimator.setInterpolator(new AccelerateInterpolator());
        mAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mCardViewAdd.setVisibility(View.INVISIBLE);
                super.onAnimationEnd(animation);
                mFloatingActionButton.setImageResource(R.drawable.icon_add);
                RegisterActivity.super.onBackPressed();
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }
        });
        mAnimator.start();
    }

    @Override
    public void onBackPressed() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            animateRevealClose();
        }
    }
}
